// pages/person/cut.js
const app = getApp()
var wh = require("../../utils/wh.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    istrue: true,
    list: [],
    lists: [],
    page: 1,
    page_size: 5,
    no_data: 0,
    onearth: false, // 是否已经到达底部
    have: true,
    haves: true,
    heimg: '',
    password: '',
    modal: false,
    order_id:''
  },
  nav() {
    let istrue = this.data.istrue
    this.setData({
      istrue: !istrue,
      page: 1,
      page_size: 5,
      no_data: 0,
      onearth: false,
      have: true,
      haves: true,
      list: [],
      lists: [],
    })
    if (this.data.istrue){
    this.ColletcGoods()
    }else{
    this.CollectActivity()
    }
  },
  ordType(e) {
    console.log(e.currentTarget.dataset)
    // 跳转到拼团的详情或者其他普通类型的 
      wx.navigateTo({
        url: '../orderDetail/ordType?order_id=' + e.currentTarget.dataset.oid,
      })
  },
  // 	我的核销列表(待核销)
  ColletcGoods() {
    let that = this;
    let page_size = this.data.page_size,
      no_data = this.data.no_data,
      page = this.data.page;
    if (no_data == 0) {
      wh.Post('/api/mall.order/write_off_list', {
        page: page,
        page_num: page_size,
        status:1
      }, function (res) {
        console.log('待核销', res)
        let shop = that.data.list.concat(res.list),
          count = res.list.length;
        console.log(res)
        if (page_size > count) {
          that.data.no_data = 1;
          if (shop.length >= 3) {
            that.setData({
              onearth: true
            })
          }
        }
        that.setData({
          list: shop
        })
        if (that.data.list.length == 0) {
          that.setData({
            have: false,
            haves: true,
          });
        } else {
          that.setData({
            have: true,
            haves: true,
          });
        }
      })
    }
  },

  // 	我的核销列表(已核销)
  CollectActivity() {
    let that = this;
    let page_size = this.data.page_size,
      no_data = this.data.no_data,
      page = this.data.page;
    if (no_data == 0) {
      wh.Post('/api/mall.order/write_off_list', {
        page: page,
        page_num: page_size,
        status: 2
      }, function (res) {
        console.log('已核销', res)
        let shop = that.data.lists.concat(res.list),
          count = res.list.length;
        console.log(res)
        if (page_size > count) {
          that.data.no_data = 1;
          if (shop.length >= 3) {
            that.setData({
              onearth: true
            })
          }
        }
        that.setData({
          lists: shop
        })
        if (that.data.lists.length == 0) {
          that.setData({
            haves: false,
            have: true,
          });
        } else {
          that.setData({
            haves: true,
            have: true,
          });
        }
      })
    }
  },
  // 删除订单
  del_order(e) {
    let that = this,
      list = this.data.list,
      order_id = e.currentTarget.dataset.oid,
      inx = e.currentTarget.dataset.inx;
    wx.showModal({
      title: '',
      content: '是否删除该订单',
      success: function (res) {
        if (res.confirm) {
          wh.Post('/api/mall.order/del', {
            order_id: order_id
          }, function (res) {
            wx.showToast({
              title: '删除成功',
            });
          })
          let arr = that.data.list;
          arr.splice(inx, 1)
          that.setData({
            lists: arr
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  // 输入密码进行订单核销
  password(e) {
    this.setData({
      password: e.detail.value
    })
  },
  write_off_pass(e) {
    let that = this;
    wh.Post('/api/mall.order/write_off_pass', {
      order_id: that.data.order_id,
      password: that.data.password
    }, function (res) {
      wx.showToast({
        title: '核销成功',
        icon: 'none',
        duration: 2000,
        complete: function () {
          setTimeout(function () {
            wx.navigateTo({
              url: '../order/order?c=-1'
            })
          }, 2000);
        }
      });
    })
  },

  // 订单核销二维码
  cancel(e) {
    let that = this;
    wx.getExtConfig({
      success: function (res) {
        let heimg = res.extConfig.apiurl + '/api/mall.order/write_off_qrcode?order_id=' + e.currentTarget.dataset.oid;
        that.setData({
          modal: true,
          heimg: heimg,
          order_id: e.currentTarget.dataset.oid
        })
      }
    });
  },
  close() {
    this.setData({
      modal: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.ColletcGoods()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      no_data: 0,
      page: 1,
      page_size: 5,
      onearth: false,
      list: [],
      lists: [],
    });
    this.ColletcGoods()
    this.CollectActivity()
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let no_data = this.data.no_data,
      page = this.data.page,
      page_size = this.data.page_size;
    if (no_data == 0) {
      this.data.page = page + 1;
      this.ColletcGoods()
      this.CollectActivity()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})